package cn.yx.common.jdbc.util;

import cn.hutool.core.util.StrUtil;
import cn.yx.common.core.exception.CommonException;
import cn.yx.common.core.util.date.DateFormatUtil;
import cn.yx.common.core.util.date.DateUtil;
import cn.yx.common.core.util.io.FileUtil;
import cn.yx.common.core.util.io.IoUtil;
import cn.yx.common.jdbc.constant.message.MysqlUtilMessageKey;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

/**
 * <p>MySQL工具类</p>
 *
 * @author Wggsmart
 */
@Slf4j
public final class MysqlUtil {

    private MysqlUtil() {
    }

    /**
     * <p>全量备份MySQL数据库，不推荐使用，直接从运维角度去备份</p>
     * <p>mysqldump -uroot -proot -hlocalhost yx-cloud > /data/backup</p>
     *
     * @param command  mysqldump命令
     * @param username 用户名
     * @param password 密码
     * @param server   数据库服务器
     * @param database 数据库
     * @param savePath 保存目录
     */
    @Deprecated
    public static void dumpInLinux(String command, String username, String password, String server, String database, String savePath) {
        String destSavePath = savePath + File.separator + database;
        FileUtil.mkdirs(destSavePath);
        String filename = destSavePath + File.separator + DateFormatUtil.format(DateUtil.date()) + ".sql";
        String wholeCommand = command + " -u" + username + " -p" + password + " -h" + server + " " + database
                + " > " + filename;
        try {
            Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", wholeCommand});
        } catch (IOException e) {
            log.error("exec mysql command: {} error: {}", wholeCommand, e.getMessage());
            throw new CommonException(MysqlUtilMessageKey.EXECUTE_COMMAND_ERROR, e);
        }
    }

    /**
     * <p>全量恢复MySQL数据库，不推荐使用，直接从运维角度去恢复</p>
     * <p>mysql -hlocalhost -P3306 -uroot -proot < /data/backup/yx-cloud.sql</p>
     *
     * @param command  mysql命令
     * @param username 用户名
     * @param password 密码
     * @param server   数据库服务器
     * @param port     端口号
     * @param database 数据库
     * @param filePath 备份的数据库文件完整路径
     */
    @Deprecated
    public static void restoreInLinux(String command, String username, String password, String server, String port,
                                      String database, String filePath) {
        String wholeCommand = command + " -h" + server + " -P" + port + " -u" + username + " -p" + password + " " + database;
        try {
            Process process = Runtime.getRuntime().exec(wholeCommand);
            executeSql(process, wholeCommand, filePath);
        } catch (IOException e) {
            log.error("exec mysql command: {} error: {}", wholeCommand, e.getMessage());
            throw new CommonException(MysqlUtilMessageKey.EXECUTE_COMMAND_ERROR, e);
        }
    }

    /**
     * 执行指定路径的SQL脚本
     *
     * @param process
     * @param wholeCommand
     * @param filePath
     */
    private static void executeSql(Process process, String wholeCommand, String filePath) {
        try (OutputStream outputStream = process.getOutputStream();
             OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream)) {
            String sql = IoUtil.readText(filePath);
            if (StrUtil.isNotEmpty(sql)) {
                outputStreamWriter.write(sql);
            }
        } catch (IOException e) {
            log.error("exec mysql command: {} error: {}", wholeCommand, e.getMessage());
            throw new CommonException(MysqlUtilMessageKey.EXECUTE_COMMAND_ERROR, e);
        }
    }

}
